﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cmd;
using System.Configuration;
using STS.Schema;
using STS.Schema.Interfaces;

namespace SqlSchemaXml
{
    class Program
    {
        static void Main(string[] args)
        {
            Options opt = CommandLineParser<Options>.GetOptions();

            //DbContext.SearchServers();
            //DbContext.Servers.Refresh();
            foreach (DbServer srv in DbContext.Servers)
            {
                Console.WriteLine("Name: {0}, Version: {1}", srv.Name, srv.ServerVersion);
                Console.WriteLine("Databases:");
                foreach (IDatabase db in srv.DatabaseList)
                {
                    Console.WriteLine("  Database: {0}", db);
                    Console.ReadKey();
                    Console.WriteLine("  Tables:");
                    foreach (ITable table in db.TableList)
                    {
                        Console.WriteLine("    Table: {0}", table);
                        Console.WriteLine("      Columns:");
                        foreach (IColumn col in table.ColumnList)
                        {
                            Console.WriteLine("        Column: {0}", col.ToString());
                        }
                        Console.WriteLine("      Foreign Keys:");
                        foreach (IForeignKey fk in table.ForeignKeyList)
                        {
                            Console.WriteLine("        FK: {0}", fk.ToString());
                            Console.WriteLine("          Foreign Key Columns:");
                            foreach (IForeignKeyColumn fkCol in fk.Columns)
                            {
                                Console.WriteLine("            FK Column: {0}", fkCol.ToString());
                            }
                        }
                        Console.WriteLine("      Triggers:");
                        foreach (ITrigger tr in table.TriggerList)
                        {
                            Console.WriteLine("        Trigger: {0}", tr);
                        }
                    }
                    //Console.ReadKey();
                    Console.WriteLine("  Views:");
                    foreach (IView view in db.ViewList)
                    {
                        Console.WriteLine("    View: {0}", view);
                    }
                    //Console.ReadKey();
                    Console.WriteLine("  Stored procedures:");
                    foreach (IStoredProcedure sp in db.StoredProcedureList)
                    {
                        Console.WriteLine("    SP: {0}", sp);
                    }
                    //Console.ReadKey();
                    Console.WriteLine("  Table functions:");
                    foreach (ITableValuedFunction tf in db.TableFunctionList)
                    {
                        Console.WriteLine("    TF: {0}", tf);
                    }
                    //Console.ReadKey();
                    Console.WriteLine("  Scalar functions:");
                    foreach (IScalarFunction sf in db.ScalarFunctionList)
                    {
                        Console.WriteLine("    SF: {0}", sf);
                    }
                    Console.ReadKey();


                    //List<ISchemaObject> schemaList = srv.SchemaFactory.GetSchema(null, null);
                }
            }

            Console.ReadKey();
        }


    }
}
